package com.lm.paymentadmin.controller;

import com.lm.paymentadmin.common.Result;
import com.lm.paymentadmin.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;


/**
 * @author liuming
 */
@Slf4j
@RestController
@RequestMapping("common")
@ApiIgnore
public class CommonController {


    @Resource
    private CommonService commonService;

    /**
     * 上传文件至阿里云 oss
     *
     * @param file
     * @return Result
     */
    @RequestMapping(value = "/uploadOSS", method = {RequestMethod.POST}, produces = {MediaType.APPLICATION_JSON_VALUE},consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result uploadOSS(@RequestParam(value = "file") MultipartFile file,
                            @RequestParam(value = "appSource")String appSource) {
        log.info("上传OSS fileName:"+file.getOriginalFilename());
        try {
            return commonService.uploadOSS(file,appSource);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 删除文件至阿里云 oss
     * @param fileName
     * @return Result
     */
    @RequestMapping("/deleteOSS")
    public Result deleteOSS(String fileName){
       log.info("删除OSS fileName:"+fileName);
        try {
            return commonService.delete(fileName);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

}
